package com.service;

import com.entity.Cart;
import com.mapper.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 购物车 服务类
 */
@Service
@Transactional
public class CartService {
    @Autowired
    private CartMapper cartMapper;

    /**添加购物车*/
    public Integer insertCart(Cart cart){
        if(cartMapper.isExistCommByCommidUserid(cart.getCommid(), cart.getUserid())==0)
        {
            cart.setCartstatus(1);
            cart.setNumber(1);
            return cartMapper.insertCart(cart);
        }else {
            cart.setCartstatus(1);
            cartMapper.updateCart(cart);
            return cartMapper.setCartNumber(cartMapper.findCommByCommidUserid(cart.getCommid(), cart.getUserid()) + 1
                    , cart.getCommid(), cart.getUserid());
        }
    }
    /**分页查看所有购物车内容*/
    public List<Cart> queryAllCart(Integer page, Integer count, String userid){
        return cartMapper.queryAllCart(page,count,userid);
    }
    /**修改购物车物品状态*/
    public Integer updateCart(Cart cart){
        return cartMapper.updateCart(cart);
    }
    /**查询商品是否被用户加购*/
    public Cart queryCartStatus(Cart cart){
        return cartMapper.queryCartStatus(cart);
    }
    /**查询我的购物车商品总数*/
    public Integer queryCartCount(String userid){
        return cartMapper.queryCartCount(userid);
    }
    /**从购物车移除商品*/
    public Integer deleteFromCart(Cart cart){
        cartMapper.setCartNumberById(0, cart.getId());
        return cartMapper.updateCart(cart);
    }
    /**添加购物车*/
    public Integer addCart(Cart cart){
        return cartMapper.setCartNumberById(cartMapper.findCommById(cart) + 1
                , cart.getId());
    }
    /**减少购物车*/
    public Integer decreaseCart(Cart cart){
        if(cartMapper.findCommById(cart) > 1) {
            return cartMapper.setCartNumberById(cartMapper.findCommById(cart) - 1
                    , cart.getId());
        }
        else {
            cartMapper.setCartNumberById(0, cart.getId());
            cart.setCartoperate(2);
            return cartMapper.updateCart(cart);
        }

    }
}
